<?php

class ContentConfiguration extends Configuration {
	protected $AppName = "content";
	protected $UseApps = array(array("name" => "mysql"),
								 array("name" => "smarty"),
								 array("name" => "content"),
								 array("name" => "account"),
								 array("name" => "redirector"));
	
	function registerMethods(AdminController $admin){
		$admin->register($this->AppName, "post", "postContentItem", null);						// DONE
		$admin->register($this->AppName, "edit", "editContentItem", null);						// DONE
		$admin->register($this->AppName, "delete", "deleteContentItems", null);					
		$admin->register($this->AppName, "add", "addContentItem", $this->Language->get("add_article"));				// DONE
		$admin->register($this->AppName, "edit-list", "editContentList", $this->Language->get("edit_article"));		// DONE
		$admin->register($this->AppName, "delete-list", "deleteContentList", $this->Language->get("delete_articles"));// DONE

	}
	
	function addContentItem(){
		$Smarty = $this->UseApps["smarty"];
		$Smarty->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}other/javascript/tinymce/tiny_mce.js\"></script>");
		$Smarty->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/content/content.js\"></script>");
		$Smarty->addHeadTag("<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}lib/content/views/content.css\" />");
		
		$this->ViewFile = "config_add.tpl";
		return true;
	}
	
	function postContentItem(){
		if(empty($_POST["title"]) || empty($_POST["content"]) || empty($_POST["allow_tags"])){
			return $this->Language->get("incomplete_article");
		}
		
		// add new post
		if(isset($_POST["addContentItem"])){
			$NewID = $this->createOrUpdateArticle();
		}
		elseif (isset($_POST["updateContentItem"])){
			$URLPosition = SystemRegistry::getInstance()->get("url_position");
			$HTTP = new HTTPRequest();
			$ID = $HTTP->get($URLPosition+3);
			
			if(is_numeric($ID)){
				$NewID = $this->createOrUpdateArticle($ID);
			}
			else {
				return;
			}
		}
		else {
			return;
		}

		$RedirectTo = SystemRegistry::getInstance()->get("url")."admin/configure/content/edit/".$NewID."/";
		$RedirectorRequest = new Request("redirector", "init", $RedirectTo);
    	RequestHandler::interrupt($RedirectorRequest);
	}
	
	function editContentItem(){
	 $URLPosition = SystemRegistry::getInstance()->get("url_position");
	 $HTTP = new HTTPRequest();
	 $ID = $HTTP->get($URLPosition+3);
	 
	 if(!is_numeric($ID)){
	   return;
   }
  
    $SmartyC = $this->UseApps["smarty"];
		$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}other/javascript/tinymce/tiny_mce.js\"></script>");
		$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/content/content.js\"></script>");
		$SmartyC->addHeadTag("<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}lib/content/views/content.css\" />");
  
   $ContentController = $this->UseApps["content"];
   $ContentItem = $ContentController->loadContent($ID);
   
   $Smarty = $SmartyC->getSmarty();
   
   $Smarty->assign("content_edit_item", $ContentItem);
   
   $this->ViewFile = "config_edit.tpl";
   return true;
  }
  
  function editContentList(){
	$this->selectAllArticles();
  	
  	$this->ViewFile = "config_edit_list.tpl";
  	return true;
  }
  
  function deleteContentList(){
  	$this->selectAllArticles();
  	
  	$SmartyC = $this->UseApps["smarty"];
  	$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/content/content.js\"></script>");
  	
  	$this->ViewFile = "config_delete_list.tpl";
  	return true;
  }
  
  function deleteContentItems(){
  	if(!isset($_POST["deleteContents"]) || empty($_POST["content_item"])){
  		return false;
  	}
  	
  	$ContentController = $this->UseApps["content"];
  	$Redirector = $this->UseApps["redirector"];
  	
  	foreach ($_POST["content_item"] as $item){
  		if(is_numeric($item)){
  			if(!$ContentController->deleteItem($item)){
  				return "Error deleting, probably non existing ID.";
  			}
  		}
  		else {
  			return "ID is not number.";
  		}
  	}
  	
  	$Redirector->redirect("/admin/configure/content/");
  }
  
  function createOrUpdateArticle($id = null){
	$AccountC = $this->UseApps["account"];
	$AuthorAccount = $AccountC->getAccount();
				
	$Title = escape($_POST["title"], $this->UseApps["mysql"]);
	$Text = escape($_POST["content"], $this->UseApps["mysql"]);
	$AllowTags = ($_POST["allow_tags"] == "yes") ? true : false;
	$Author = $AuthorAccount->getID();
	
	$Content = $this->UseApps["content"];
	$NewItem = $Content->addContentItem($Title, $Text, $AllowTags, $Author, array(),  $id);
		
	$NewID = $NewItem->getID();
	
	return $NewID;
  }
  
  function selectAllArticles(){
  	  	$MySQL = $this->UseApps["mysql"];
  	$Content = $this->UseApps["content"];
  	$SmartyC = $this->UseApps["smarty"];
  	$Smarty = $SmartyC->getSmarty();
  	
  	$SmartyC->addHeadTag("<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}lib/content/views/content.css\" />");
  	
  	$Select_all = $MySQL->query("SELECT * FROM {$MySQL->getPrefix()}content ORDER BY item_last_update DESC");
  	
  	$Articles = array();
  	
  	while ($item = mysql_fetch_assoc($Select_all)) {
  		$Articles[] = $Content->loadContent($item["id"]);
  	}
  	
  	$Smarty->assign("content_cfg_list", $Articles);
  }
}

?>